package com.emath.activities;

import android.app.Activity;
import android.content.Context;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;

import com.lnl.R;
import com.emath.shared.CustomTouchListener;
import com.emath.shared.Globals;

public class SettingsActivity extends Activity implements OnClickListener {
	/** Called when the activity is first created. */

	private TextView tv, tv1, tv2, tv3, tv4;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.settings);
		Typeface capture = Typeface.createFromAsset(getAssets(),
				"fonts/Capture_it.ttf");

		// Translation
		Animation trans = new TranslateAnimation(400, 0, 0, 0);
		trans.setDuration(2000); // 2 Seconds
		AnimationSet animSet = new AnimationSet(true);
		animSet.setFillEnabled(true);
		animSet.addAnimation(trans);

		tv = (TextView) findViewById(R.id.sound);
		tv1 = (TextView) findViewById(R.id.music);
		tv2 = (TextView) findViewById(R.id.vibrate);
		tv3 = (TextView) findViewById(R.id.difficulty);
		tv4 = (TextView) findViewById(R.id.back);

		tv.setTypeface(capture);
		tv1.setTypeface(capture);
		tv2.setTypeface(capture);
		tv3.setTypeface(capture);
		tv4.setTypeface(capture);

		tv.startAnimation(animSet);
		// Thread.sleep(1000);
		tv1.startAnimation(animSet);
		// Thread.sleep(1000);
		tv2.startAnimation(animSet);
		// Thread.sleep(1000);
		tv3.startAnimation(animSet);
		// Thread.sleep(1000);
		tv4.startAnimation(animSet);

		tv.setOnTouchListener(new CustomTouchListener());
		tv1.setOnTouchListener(new CustomTouchListener());
		tv2.setOnTouchListener(new CustomTouchListener());
		tv3.setOnTouchListener(new CustomTouchListener());
		tv4.setOnTouchListener(new CustomTouchListener());

		tv.setOnClickListener(this);
		tv1.setOnClickListener(this);
		tv2.setOnClickListener(this);
		tv3.setOnClickListener(this);
		tv4.setOnClickListener(this);

		setText();
	}

	private void setText() {
		tv.setText(Globals.settingsHolder[0] == 0 ? "Sound: off" : "Sound: on");
		tv1.setText(Globals.settingsHolder[1] == 0 ? "Music: off" : "Music: on");
		tv2.setText(Globals.settingsHolder[2] == 0 ? "Vibrate: off"
				: "Vibrate: on");

		if (Globals.settingsHolder[3] == 0) {
			tv3.setText("Difficulty: Easy");
		} else if (Globals.settingsHolder[3] == 1) {
			tv3.setText("Difficulty: Medium");
		} else {
			tv3.setText("Difficulty: Hard");
		}
	}

	private void getSettings() {
		// SharedPreferences settings =
		// getSharedPreferences(GameMenu.PREFS_NAME, 0);

		// settingsHolder[0] = settings.getBoolean("sound", true);
		// settingsHolder[1] = settings.getBoolean("music", true);
		// settingsHolder[2] = settings.getBoolean("vibrate", true);
		// settingsHolder[3] = settings.getBoolean("graphics", true);

	}

	@Override
	public void onClick(View v) {
		// SharedPreferences settings =
		// getSharedPreferences(GameMenu.PREFS_NAME, 0);
		// SharedPreferences.Editor editor = settings.edit();

		switch (v.getId()) {
		case R.id.sound:
			Globals.settingsHolder[0] = (Globals.settingsHolder[0] + 1) % 2;
			setText();
			if (Globals.settingsHolder[0] == 0) {
				Globals.mAudioManager = (AudioManager) this
						.getSystemService(Context.AUDIO_SERVICE);
				Globals.preMuteVolume = Globals.mAudioManager
						.getStreamVolume(AudioManager.STREAM_MUSIC);
				// If you want to player is mute ,then set_volume variable is
				// zero.Otherwise you may supply some value.
				int set_volume = 0;
				Globals.mAudioManager.setStreamVolume(
						AudioManager.STREAM_MUSIC, set_volume, 0);
			} else if (Globals.settingsHolder[0] == 1) {
				Globals.mAudioManager = (AudioManager) this
						.getSystemService(Context.AUDIO_SERVICE);
				Globals.mAudioManager.setStreamVolume(
						AudioManager.STREAM_MUSIC, Globals.preMuteVolume, 0);
			}
			break;
		case R.id.music:
			Globals.settingsHolder[1] = (Globals.settingsHolder[1] + 1) % 2;
			if (Globals.settingsHolder[1] == 0) {
				stopService(Globals.backgroundMusicService);
				Globals.isMusicPlaying = false;
			} else if (Globals.settingsHolder[1] == 1) {
				startService(Globals.backgroundMusicService);
				Globals.isMusicPlaying = true;
			}
			setText();
			// editor.putBoolean("music", !settingsHolder[1]);
			break;
		case R.id.vibrate:
			Globals.settingsHolder[2] = (Globals.settingsHolder[2] + 1) % 2;
			setText();
			if (Globals.settingsHolder[2] == 0) {
				Globals.vibrateTime = 0;
			} else if (Globals.settingsHolder[2] == 1) {
				Globals.vibrateTime = 2000;
				Vibrator vibs = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
				// Vibrate for 500 milliseconds
				vibs.vibrate(2000);

			}
			break;
		case R.id.difficulty:
			Globals.settingsHolder[3] = (Globals.settingsHolder[3] + 1) % 3;
			// editor.putBoolean("graphics", !settingsHolder[3]);
			break;
		case R.id.back:
			finish();
			break;
		}

		// editor.commit();

		// getSettings();
		setText();
	}
	@Override
	protected void onPostResume() {
		if (Globals.focusedActivity.equals("SettingsActivity")) {
			startService(Globals.backgroundMusicService);
			Globals.isMusicPlaying = true;
		}
		super.onPostResume();
	}

	@Override
	protected void onPause() {
		if (this.isFinishing()) {
			Globals.focusedActivity = "GameMenu";
		} else {
			stopService(Globals.backgroundMusicService);
			Globals.isMusicPlaying = false;
		}
		super.onPause();
	}
}